﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace lchealthy
{
    public partial class frmreport : Form
    {
        private int mid = 0;
        string mystr = "";
        public frmreport(int mmid)
        {
            mid = mmid;
            InitializeComponent();
        }

        private void frmreport_Load(object sender, EventArgs e)
        {
            //this.Text = mid.ToString();
            mystr = getresult();
            lblmessage.Text = mystr;
        }
        private string getresult()
        {
            string sr = "";
            string sql = "select [MDate],[MWeight],[MBMI],[MFatAll],[MFatUp],[MMetaAll],[MMeatUp],[MThews],[MThewType],[MWater],[MAge],[MFatIn],[MBone],[MXW],[MUBelly],[MDBelly],[Mwaistline],[MHip],[MRleg],[MRARM],[MRSlet],[MReaction],[cname],csex, cbirthday,cheight from customer INNER JOIN measureInfo ON customer.cid = measureInfo.CID where mid=" + mid;
            DataSet ds = DbHelperOleDb.Query(sql);
            if (ds.Tables[0].Rows.Count == 1)
            {
                DataRow r = ds.Tables[0].Rows[0];
                this.Text = ds.Tables[0].Rows[0]["cname"].ToString() + DateTime.Parse(ds.Tables[0].Rows[0][0].ToString()).ToLongDateString() + "测量记录";
                string s, sex;
                sex = r["csex"].ToString();
                int age = myfun.getAge(DateTime.Parse(r["cbirthday"].ToString()));
                float h = 0f, zf = 0f, water = 0f, bmi = 0f, dx = 0f, weight = 0f;
                s = r["cheight"].ToString();
                if (s != "")
                    h = float.Parse(s);
                s = r["MFatAll"].ToString();
                if (s != "")
                    zf = float.Parse(s);
                s = r["MWater"].ToString();
                if (s != "")
                    water = float.Parse(s);
                s = r["MBMI"].ToString();
                if (s != "")
                    bmi = float.Parse(s);
                s = r["MMetaAll"].ToString();
                if (s != "")
                    dx = float.Parse(s);
                s = r["MWeight"].ToString();
                if (s != "")
                    weight = float.Parse(s);
                sr = "身高：" + r["cheight"] + "CM\r\n";
                sr += "体重：" + weight + "公斤  标准体重：" + myfun.getWeight(h, sex).ToString("F2") + "公斤\r\n";
                sr += "BMI：" + bmi.ToString() + " " + myfun.getBMI(bmi) + "\r\n";
                sr += "脂肪：" + r["MFatAll"] + " " + myfun.getZF(sex, age, zf) + "\r\n";
                sr += "代谢：" + r["MMetaAll"] + " 代谢差为：" + myfun.getDX(weight, dx) + "\r\n";
                sr += "肌肉：" + r["MThews"] + "\r\n";
                sr += "体型：" + r["MThewType"].ToString() + " " + myfun.getTX(int.Parse(r["MThewType"].ToString())) + "\r\n";
                sr += "水分：" + r["MWater"] + " " + myfun.getWater(sex, water) + "\r\n";
                sr += "生理年龄：" + r["MAge"] + "\r\n";
                sr += "内脂肪：" + r["MFatIn"] + " " + myfun.getNZ(int.Parse(r["MFatIn"].ToString())) + "\r\n";
                sr += "骨量：" + r["MBone"] + "\r\n";
                sr += "胸围：" + r["MXW"] + "\r\n";
                sr += "上腹：" + r["MUBelly"] + "\r\n";
                sr += "下腹：" + r["MDBelly"] + "\r\n";
                sr += "腰围：" + r["Mwaistline"] + "\r\n";
                sr += "臀围：" + r["MHip"] + "\r\n";
                sr += "右腿围：" + r["MRleg"] + "\r\n";
                sr += "右臂围：" + r["MRARM"] + "\r\n";
                sr += "右小腿围：" + r["MRSlet"] + "\r\n";
                sr += "骨量：" + r["MBone"] + "\r\n";

            }
            return sr;
        }
        private void btnexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            printDocument1.Print();

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top; //上边距
            string line =mystr;//行字符串
            Font printFont = this.lblmessage.Font; //当前的打印字体
            SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);
            //每页可打印的行数
            //逐行的循环打印一页
            string[] ss =Regex.Split(line,"\r\n");
            foreach (string l in ss)
            {
                yPosition = topMargin + (count * printFont.GetHeight(g));
                g.DrawString(l, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }
            



        }


    }
}
